package com.lazycat.backend.service.impl.shop;

import com.alibaba.fastjson.JSONObject;
import com.lazycat.backend.mapper.GoodMapper;
import com.lazycat.backend.pojo.Good;
import com.lazycat.backend.service.shop.FetchAllGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

//update 11.30
@Service
public class FetchAllGoodsServiceImpl implements FetchAllGoodsInfoService {
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public JSONObject fetch() {
        JSONObject resp = new JSONObject();
        // BriefItemGood列表
        ArrayList<JSONObject> items = new ArrayList<>();
        // 查询所有数据
        List<Good> goods = goodMapper.selectList(null);
        for (Good good : goods) {
            if (good.getBuyerId() == -2 || good.getBuyerId() >= 0) {
                continue;
            }
            JSONObject item = new JSONObject();
            item.put("img", Base64.getEncoder().encodeToString(good.getGoodImg()));
            item.put("name", good.getGoodName());
            item.put("price", good.getGoodPrice());
            item.put("id", good.getId());
            items.add(item);
        }

        resp.put("items", items);
        resp.put("error_message", "success");
        return resp;
    }
}
